<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div class="container">
        <el-card shadow="hover" style="padding:0">
            
            <!-- 操作，单条件查询 -->
            <el-row class="simple_search_bar_wrapper">
                <el-col :xs="12" :sm="12" :lg="8">
                    <el-form class="searchForm" ref="searchForm" :model="searchForm">
                        <el-form-item prop="searchValue">
                            <el-input placeholder="请输入关键字" v-model="searchForm.searchValue" clearable @clear="handleClearKeyword" class="input-with-select" size="medium">
                                <el-select no-data-text="暂无数据" slot="prepend" placeholder="查询字段" v-model="searchForm.searchKey" clearable @clear="handleClearSelect" size="medium">
                                    <el-option
                                        v-for="item in searchColumns"
                                        :key="item.key"
                                        :label="item.label"
                                        :value="item.key"
                                    ></el-option>
                                </el-select>
                                <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
                            </el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
                
                <el-col :xs="12" :sm="12" :lg="16" class="btn_wrapper">
                    <el-button type="primary" icon="el-icon-plus" size="medium" @click="handleOpenDialog('create')">添加</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="medium" @click="handleDelete">删除</el-button>
                    <el-button type="primary" icon="el-icon-s-open" size="medium" @click="condition = !condition">过滤</el-button>
                    <el-button type="primary" icon="el-icon-refresh" size="medium" @click="handleResetSearch('searchForm')">刷新</el-button>
                </el-col>
            </el-row>

            <!-- 多条件过滤查询 -->
            <el-row class="condition_search_wrapper margin-bottom-10" v-show="condition">
                <el-col :span="20">
                    <el-form class="conditionSearchForm" ref="conditionSearchForm" :model="searchForm">
                        <el-row :gutter="10">
                            <el-col :xs="12" :sm="12" :lg="4">
                                <el-form-item prop="searchValue">
                                    <el-input placeholder="请输入关键字" clearable size="medium"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :xs="12" :sm="12" :lg="4">
                                <el-form-item prop="searchValue">
                                    <el-input placeholder="请输入关键字" clearable size="medium"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :xs="12" :sm="12" :lg="4">
                                <el-form-item prop="searchValue">
                                    <el-input placeholder="请输入关键字" clearable size="medium"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :xs="12" :sm="12" :lg="4">
                                <el-form-item prop="status">
                                    <el-select v-model="searchForm.status" clearable size="medium" placeholder="状态">
                                        <el-option label="进行中" :value="true"></el-option>
                                        <el-option label="已结束" :value="false"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :xs="12" :sm="12" :lg="8">
                                <el-form-item>
                                    <el-date-picker 
                                        size="medium" clearable
                                        v-model="daterange"
                                        type="daterange"
                                        range-separator="至"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-col>

                <el-col :span="4" class="search_btn">
                    <el-row :gutter="10">
                        <el-col :xs="24" :sm="24" :lg="12" class="search_btn">
                            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleConditionSearch">查询</el-button>
                        </el-col>
                        <el-col :xs="24" :sm="24" :lg="12" class="search_btn">
                            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleResetSearch('conditionSearchForm')">重置</el-button>
                        </el-col>
                    </el-row>
                </el-col>
            </el-row>

            <!-- 表格 -->
            <el-table
                ref="table" size="small" empty-text="暂无数据" max-height="600"
                element-loading-text="给我一点时间"
                fit stripe border highlight-current-row
                v-loading="loading"
                :data="datas"
                :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                @selection-change="handleSelection">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" width="55" align="center"></el-table-column>
                
                <el-table-column label="ID" prop="id" column-key="id" align="center"/>
                <el-table-column label="名称" prop="name" column-key="name" align="center"/>
                <el-table-column label="编码" prop="code" column-key="code" align="center"/>
                <el-table-column label="备注" prop="remarks" column-key="remarks" align="center"/>
                <el-table-column label="创建时间" prop="ctime" column-key="ctime" align="center"/>
                <el-table-column label="最后更新时间" prop="utime" column-key="utime" align="center"/>
                
                <!--
                <el-table-column label="状态" width="80px" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.status === true">正常</el-tag>
                        <el-tag type="danger" size="small" v-else>禁用</el-tag>
                    </template>
                </el-table-column>
                -->
                
                
                <el-table-column label="操作" width="250px" align="center" fixed="right">
                    <template slot-scope="scope">
                        <div class="table_action">
                            <el-button type="primary" size="small" @click="handleOpenDialog('update', scope.row)">编辑</el-button>
                            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                            <div class="table_action_dropdown">
                                <el-dropdown split-button type="primary" size="small" style="margin-left: 10px;">
									操作
                                    <el-dropdown-menu slot="dropdown">
                                        <el-dropdown-item>黄金糕</el-dropdown-item>
                                        <el-dropdown-item>狮子头</el-dropdown-item>
                                        <el-dropdown-item>螺蛳粉</el-dropdown-item>
                                        <el-dropdown-item>双皮奶</el-dropdown-item>
                                        <el-dropdown-item>蚵仔煎</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页工具栏 -->
            <div class="pagination">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="searchForm.page"
                        :page-sizes="[10, 20, 30, 50]"
                        :page-size="searchForm.rows"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>

            <!-- 窗口 -->
            <el-dialog
                :title="actionTypeMap[actionType]"
                :visible.sync="dialogVisible"
                @close="handleDialogClose">
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                	
                    <el-form-item label="ID" prop="id">
                        <el-input v-model="form.id" placeholder="ID" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name" placeholder="名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="编码" prop="code">
                        <el-input v-model="form.code" placeholder="编码" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remarks">
                        <el-input v-model="form.remarks" placeholder="备注" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="ctime">
                        <el-input v-model="form.ctime" placeholder="创建时间" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="最后更新时间" prop="utime">
                        <el-input v-model="form.utime" placeholder="最后更新时间" style="width: 80%;"></el-input>
                    </el-form-item>
                    
                    <!--
                    <el-select style="width: 100%;" v-model="form.sex" placeholder="员工性别">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="2"></el-option>
                    </el-select>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="1" :max="10000" placeholder="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="备注" prop="remarks">
                        <el-input type="textarea" :rows="2" placeholder="请输入内容" style="width: 80%;" v-model="form.remarks"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="true">正常</el-radio>
                            <el-radio :label="false">锁定</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    -->
                    
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button @click="handleSubmit('form')" type="primary" :loading="submitLoading">提 交</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage } from "@/api/admin/sysrole"
import { parseTime } from "@/libs/utils"
import { ResultMessage } from "@/libs/response"
import valid from "@/libs/rules"
export default {
    data() {
        return {
            actionType: 'create',
            actionTypeMap: { create: '新增', update: '编辑' },
            dialogVisible: false,
            loading: true,
            submitLoading: false,
            condition: false,
            total: 0,
            datas: [],
            searchForm: {
                page: 1,
                limit: 10,
                searchKey: '',
                searchValue: '',
                daterange: null,
                status: null
            },
            daterange: [],
            searchColumns: [],
            form: {
                id: null,
                name: null,
                code: null,
                remarks: null,
                ctime: null,
                utime: null,
            	sort: 99
            },
            rules: {
                name: [valid.required()]
            }
        }
    },
    mounted() {
        this.setSearchColumnsKey()
        this.getSelectPage()
    },
    methods: {
        handleOpenDialog(type, row, index) { // 打开表单窗口
            this.actionType = type
            this.dialogVisible = true
            if (type === 'update') {
                this.getOne(row.id).then(res => {
                    this.form = Object.assign({}, res.data.data)
                })
            }
        },
        handleDialogClose() { //表单窗口关闭
            this.handleReset('form')
        },
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getSelectPage()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getSelectPage()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.daterange = []
            this.handleReset(name)
            this.getSelectPage()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        setSearchColumnsKey() { // 设置查询条件Key
            this.searchColumns = []
            for (let column of this.$refs.table.columns) {
                if (column.columnKey) {
                    this.searchColumns.push({
                        key: column.columnKey,
                        label: column.label
                    })
                }
            }
        },
        handleClearSelect() { // 清空搜索下拉框
            this.searchForm.searchKey = null
        },
        handleClearKeyword() { // 清空关键字
            this.searchForm.searchValue = null
            this.getSelectPage()
        },
        handleSearch() { // 单条件搜索
            if (this.searchForm.searchValue != null && this.searchForm.searchValue.trim() === '') {
                this.searchForm.searchValue = null
            }
            let searchFormDefault = Object.assign({}, this.searchForm)
            let search = Object.assign({}, this.searchForm)
            this.searchForm[this.searchForm.searchKey] = this.searchForm.searchValue
            this.getSelectPage()
            this.searchForm = searchFormDefault //恢复条件查询默认值
        },
        handleConditionSearch() { // 多条件组合查询
            console.log(this.searchForm);
        },
        

        handleSubmit(name) { // 提交表单
        	const refs = this.$refs
            refs[name].validate((valid) => {
                if (valid) {
                    this.submitLoading = true
                    if (this.actionType === 'create') {
                        save(this.form).then(res => {
                            if (ResultMessage(res)) {
                                this.submitLoading = false
                                this.dialogVisible = false
                                this.handleReset('form')
                                this.getSelectPage()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else if (this.actionType === 'update') {
                        updated(this.form).then(res => {
                            if (ResultMessage(res)) {
                                this.submitLoading = false
                                this.dialogVisible = false
                                this.handleReset('form')
                                this.getSelectPage()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else {
                        console.log('未知操作');
                    }
                }
            })
        },

        handleDelete(row) { // 删除
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted({ idList: ids }).then(response => {
                        if (ResultMessage(response)) {
                            this.getSelectPage()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

        getOne(id) { // 根据ID加载数据
            if (id !== undefined) {
                return info(id).then(res => {
                    if (ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },

        getSelectPage() { // 加载列表数据
            selectMyPage(this.searchForm).then(res => {
                if (ResultMessage(res, false)) {
                    this.datas = res.data.data.records
                    this.total = res.data.data.total
                    this.loading = false
                } else {
                    this.loading = false
                }
            }).catch(error => {
                this.loading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        },
    }
}
</script>